﻿// // ******************************************************************
// //       /\ /|       @file       MediaRepository.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-31 20:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-27 18:12 Await
// // ******************************************************************

using AutomaticSubtitles.Service.DataAccess.Model;
using AutomaticSubtitles.Service.Domain.Media;

namespace AutomaticSubtitles.Service.Infrastructure.Repositories;

public class MediaRepository(AutoDbContext context, IUnitOfWork unitOfWork, IStringLocalizer<SharedResource> localizer) : BaseRepository<AutoDbContext, Auto_Media, string>
    (context, unitOfWork, localizer), IMediaRepository
{
    public async Task AddRange(List<Auto_Media> newMedias)
    {
        if (newMedias is { Count: 0 }) return;
        await Context.AddRangeAsync(newMedias);
        await Context.SaveChangesAsync();
    }

    public async Task UpdateRange(List<Auto_Media> updatedMedias)
    {
        if (updatedMedias is { Count: 0 }) return;
        Context.UpdateRange(updatedMedias);
        await Context.SaveChangesAsync();
    }

    public async Task RemoveRange(List<Auto_Media> deletedMedias)
    {
        if (deletedMedias is { Count: 0 }) return;
        Context.ChangeTracker.Clear();
        Context.RemoveRange(deletedMedias);
        await Context.SaveChangesAsync();
    }

    public Task<List<Auto_Media>> GetList()
    {
        return Context.AutoMedias.Where(x => true).ToListAsync();
    }

    public Task<List<string>> GetMediaRandomData()
    {
        return Context.AutoMedias
            .Where(x => x.Leve == 0)
            .OrderBy(x => EF.Functions.Random())
            .Take(10)
            .Select(x => x.Id)
            .ToListAsync();
    }
}